﻿using AutoMapper;
using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Exports;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using System.Net.Http;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.库存管理
{
    /// <summary>
    /// 库存管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "PC")]
    [ApiController]
    public class InventoryController : BaseController
    {
        private readonly IInventoryService inventoryService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="inventoryService"></param>
        public InventoryController(IWebHostEnvironment environment, IInventoryService inventoryService) : base(environment)
        {
            this.inventoryService = inventoryService;
        }

        /// <summary>
        /// 库存明细
        /// </summary>
        /// <param name="whId"></param>
        /// <param name="mname"></param>
        /// <param name="status"></param>
        /// <param name="locationcode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryDetailList(long whId = 0, string? mname = "", int status = 0, string? locationcode = "", int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = inventoryService.GetInventoryDetailList(whId, mname, status, locationcode, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据仓位编码获取该仓位库存信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryByCellCode(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }

            var cobj = inventoryService.GetInventoryByCellCode(code);
            return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
        }

        /// <summary>
        /// 库存汇总
        /// </summary>
        /// <param name="whId"></param>
        /// <param name="mname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryGroupList(long whId = 0, string? mname = "", int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = inventoryService.GetInventoryGroupList(whId, mname, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据物料编码获取库存信息（PDA使用）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryPalletDto(string? key, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var res = inventoryService.GetInventoryPalletDto(key, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }

        /// <summary>
        /// 导出库存明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DowLoadExcel(int status)
        {
            var list = inventoryService.GetInventoryExport(status);
            if (list != null)
                return BaseDowLoadExcel(list);
            else
                return Ok();
        }

        /// <summary>
        /// 每日库存快照
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult InventoryStockDown()
        {
            var list = inventoryService.GetInventoryMsgDown();
            if (list != null)
                return InventoryStockDown(list);
            else
                return Ok();
        }


        /// <summary>
        /// 获取入库在途库存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventroyIn()
        {
            var list = inventoryService.GetInventroyIn();
            return Ok(BaseResult.Result(list, "200"));
        }

        /// <summary>
        /// 获取盘点可用库位
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryStockeCell(int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = inventoryService.GetInventoryStockeCell( pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }




    }
}
